package com.itheima.service.impl;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.JobCountData;
import com.itheima.pojo.JobOption;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private EmpMapper empMapper;

    /**
     * 查询员工职位数据
     * 【推荐】使用实体类来封装sql返回的数据
     * @return
     */
    //@Override
    public JobOption empJobData2() {
        //返回的数据
        JobOption jobOption = new JobOption();
        List<JobCountData> jobCountDataList = empMapper.countEmpJobData2();
        //设置jobList, JobCountData转成字符串
        List<String> jobList = jobCountDataList.stream()// filter,map
            // 转换类型
            .map(JobCountData::getJobName)//流中数据类型string, 教研主管,讲师...
            // 收集起来，用集合来装，list
            .toList();
        /*List<String> jobList = new ArrayList<>();
        for (JobCountData jobCountData : jobCountDataList) {
            jobList.add(jobCountData.getJobName());
        }*/
        jobOption.setJobList(jobList);

        //设置dataList
        List<Integer> dataList = jobCountDataList.stream().map(JobCountData::getNum).toList();
        /*List<Integer> dataList = new ArrayList<>();
        for (JobCountData jobCountData : jobCountDataList) {
            dataList.add(jobCountData.getNum());
        }*/
        jobOption.setDataList(dataList);

        return jobOption;
    }

    // 使用map的缺点：硬编码，不便于阅读。优点：不需要创建实体类
    @Override
    public JobOption empJobData() {
        //返回的数据
        JobOption jobOption = new JobOption();

        //统计查询
        List<Map> jobCountDataList = empMapper.countEmpJobData();
        List<String> jobList = new ArrayList<>();
        for (Map map : jobCountDataList) {
            // 取出岗位名称, pos ???
            jobList.add(map.get("pos").toString());
        }
        jobOption.setJobList(jobList);

        //设置dataList
        List<Integer> dataList = new ArrayList<>();
        for (Map map : jobCountDataList) {
            // 获取岗位数量
            dataList.add(((Long)map.get("total")).intValue());
        }
        jobOption.setDataList(dataList);

        return jobOption;
    }

    /**
     * 查询员工性别数据
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> empGenderData() {
        return empMapper.empGenderData();
    }
}
